<template>
  <div>
    <van-picker
      title="性别"
      show-toolbar
      :default-index="value"
      :columns="columns"
      @confirm="onConfirm"
      @cancel="$emit('on-cancel')"
    />
  </div>
</template>

<script>
import { setUserProfile } from '@/api/user.js'
export default {
  data() {
    return {
      columns: ['男', '女']
    }
  },

  props: {
    value: {
      type: Number,
      required: true
    }
  },

  created() {

  },

  methods: {
    async onConfirm(gender) {
      gender = gender === '男' ? 0 : 1
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '更改中...'
      })
      try {
        await setUserProfile({
          gender
        })
        this.$toast('修改成功')
        this.$emit('on-cancel')
        this.$emit('input', gender)
      } catch (err) {
        this.$toast('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
